
package com.zhongxi.guoyuan.view.coinShopping;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.coinStore.CoinOrderData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.utils.MyTextUtils;

import java.util.HashMap;
import java.util.Map;

public class CoinCommentActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    private CoinOrderData list;
    private TextView tvPhone;
    private ImageView ivImg;
    private TextView tvName;
    private TextView tvMoney;
    private TextView tvOrderTime;
    private Button btSubscribe;
    private RatingBar rb;
    private TextView tvScore;
    private Map<String, String> map = new HashMap<>();
    private String star = "5";
    private EditText etTips;
    private TextView tvNum;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_coin_comment);
        initTitle();
        tvTitle.setText("评价页面");
        Intent intent = getIntent();
        list = (CoinOrderData)intent.getSerializableExtra("info");
        ivImg = (ImageView) findViewById(R.id.ivImg);
        tvName = (TextView) findViewById(R.id.tvName);
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvMoney = (TextView) findViewById(R.id.tvMoney);
        tvOrderTime = (TextView) findViewById(R.id.tvOrderTime);
        tvScore = (TextView) findViewById(R.id.tvScore);
        tvNum = (TextView) findViewById(R.id.tvNum);
        etTips = (EditText) findViewById(R.id.etTips);
        btSubscribe = (Button) findViewById(R.id.btSubscribe);
        rb = (RatingBar) findViewById(R.id.rb);
        setData();
    }

    /**
     * 初始化数据
     */
    private void setData() {
        if(null!=list.getShopthumbnail()&&!"".equals(list.getShopthumbnail())){
//            ImageLoader.getInstance().displayImage(Url.IMG_URL+list.getShopthumbnail(), ivImg, CommonTool.getOptions(R.mipmap.iv_eg));
            Glide.with(context).load(Url.IMG_URL+list.getShopthumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(ivImg);
        }
        if(null!=list.getShopname()&&!"".equals(list.getShopname())){
            tvName.setText(list.getShopname());
        }else{
            tvName.setText("");
        }
        if(null!=list.getShopphone()&&!"".equals(list.getShopphone())){
            tvPhone.setText("联系商家："+list.getShopphone());
        }else{
            tvPhone.setText("联系商家：******");
        }
        if(null!=list.getPrice()&&!"".equals(list.getPrice())){
            tvMoney.setText("付款金额："+list.getPrice()+"银币");
        }else{
            tvMoney.setText("付款金额：***银币");
        }
        if(null!=list.getCtime()&&!"".equals(list.getCtime())){
            tvOrderTime.setText("付款时间："+list.getCtime());
        }else{
            tvOrderTime.setText("付款时间：***");
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        btSubscribe.setOnClickListener(this);
        etTips.addTextChangedListener(this);
        rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener()
        {
            public void onRatingChanged(RatingBar ratingBar, float rating,  boolean fromUser) {
                star = rating+"";
                tvScore.setText(rating+"");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.btSubscribe:
                if(!MyTextUtils.isFastDoubleClick()) {
                    String content = etTips.getText().toString().trim();
                    if (TextUtils.isEmpty(content)) {
                        showShortToast("评论内容不能为空！");
                    } else {
                        toComment(content);
                    }
                }
                break;
        }
    }

    /**
     * 评论
     * @param content
     */
    private void toComment(String content) {
        map.clear();
        map.put("shopid", list.getShopid());
        map.put("orderid", list.getId());
        map.put("starlevel", star);
        map.put("msg", content);
        getServer(Url.ADD_COMMENT, map, "", false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        MyApplication.getInstance().setSpValue(result.getSafeobj());
        showShortToast("评论成功");
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        showShortToast("评论失败");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable content) {
        MyTextUtils.TextChangeUtils(context, tvNum, content);
    }
}
